package com.nuaa.likesystem.domain;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nuaa.likesystem.constant.ThumbConstant;
import com.nuaa.likesystem.mapper.ThumbMapper;
import com.nuaa.likesystem.model.entity.Thumb;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author: ortonzhu
 * @data 2025年04月23日 14:45
 * @Description
 */
@Component
@Slf4j
public class ThumbDomain {

	@Resource
	private RedisTemplate<String, Object> redisTemplate;

	@Resource
	private ThumbMapper thumbMapper;

	/**
	 * 判断是否已经点赞
	 *
	 * @param blogId
	 * @param userId
	 * @return
	 */
	public Boolean hasThumb(Long blogId, Long userId) {
		Boolean exist = redisTemplate.opsForHash()
		                             .hasKey(ThumbConstant.USER_THUMB_KEY_PREFIX + userId, blogId.toString());
		if (!exist) {
			Thumb thumb = thumbMapper.selectOne(new LambdaQueryWrapper<Thumb>().eq(Thumb::getUserId, userId)
			                                                                   .eq(Thumb::getBlogId, blogId));
			return thumb != null;
		}
		return true;
	}

}
